<?php

declare(strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use think\response\Json;
//该文件为自动生成，请按需修改
class Activity extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Activity';

    protected $append = ['status_text'];

    public function index(): string
    {
        return Builder::Table()
            ->autoStructure($this->model, $this->pk, 'barDemo,300')
            ->addRightButtons([
                'edit' => ['size' => '80%'],
                'actdetail' =>
                [
                    'size' => '100%',
                    'title' => '报名情况',
                    'url'   => url('userActivity/index')->build(),
                    'site'        => 'r',
                    'event' => '_openDrawer'
                ],
                'delete'
            ])
            ->addTopButtons(['add' => ['size' => '80%'], 'deleteAll'])
            ->fetch();
    }

    public function save($param = [], $model = false): Json
    {
        $time = time();
        $param = $this->request->post();
        if(empty($param['bm_time'])){
            return json(['code' => -1, 'msg' => '活动报名时间不能为空']);
        }
        if(empty($param['hd_time'])){
            return json(['code' => -1, 'msg' => '活动开始时间不能为空']);
        }
        $hdtime = explode(' - ', $param['hd_time']);
        $param['hd_s_time'] = strtotime($hdtime[0]);
        $param['hd_e_time'] = strtotime($hdtime[1]);
        $bmtime = explode(' - ', $param['bm_time']);
        $param['bm_s_time'] = strtotime($bmtime[0]);
        $param['bm_e_time'] = strtotime($bmtime[1]);
        if ($bmtime[1] > $hdtime[0]) {
            return json(['code' => -1, 'msg' => '活动报名结束时间不可大于活动开始时间']);
        }
        if($param['hd_s_time']>$time){
            $param['status'] = 1;
        }else if($param['hd_e_time']>$time && $param['hd_s_time']<$time){
            $param['status'] = 2;
        }else{
            $param['status'] = 3;
        }
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }
}
